﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class SupportNumShelfController : BaseController<waresdetails>
    {
        //
        // GET: /SupportNumShelf/

        public override BLL.BaseBll<waresdetails> bll
        {
            get
            {
                return new WaresDetailsBll();
            }

        }
        //waresName  waresID   storeName waresCode waresBatch waresquantity waresSupportNum
        public class SupportModel
        {
            public string waresName;
            public long waresID;
            public string storeName;
            public string waresCode;
            public string waresBatch;
            public int validstatus;
            public int? waresquantity;
            public int? waresSupportNum;
            public int? waresMinNum;
        }


        StoreBll storeBll = new StoreBll();
        WaresBll waresBll = new WaresBll();

        public ActionResult Get2(int page, int rows, waresdetails model, string name,string storeid)
        {
            var listSup = new List<SupportModel>();
            Expression<Func<waresdetails, bool>> condition = x => x.storeid == 10010 && x.validstatus == 1;
            int count;
            string waresName = "";
            int storeID = 0;

            if (storeid != ""&&storeid!=null && String.IsNullOrEmpty(name))
            {
                storeID=Convert.ToInt32(storeid);
                condition = x => x.storeid == storeID && x.validstatus == 1;
                var list = bll.Search(condition, true, x => x.waresID, rows, page, out count).ToList().Select(x => new
                {
                    x.waresID,
                    x.waresCode,
                    x.storeid,
                    x.waresBatch,
                    x.waresquantity,
                    x.waresSupportNum,
                    x.validstatus,
                    x.waresMinNum,
                }).ToList();
                foreach (var item in list)
                {
                    waresName = waresBll.Search(x => x.waresCode == item.waresCode).Select(x => new { x.waresName }).FirstOrDefault().waresName;
                    string storeName = storeBll.Search(x => x.storeid == item.storeid).Select(x => new { x.storeName }).FirstOrDefault().storeName;
                    SupportModel supModel = new SupportModel();
                    supModel.waresID = item.waresID;
                    supModel.waresCode = item.waresCode;
                    supModel.storeName = storeName;
                    supModel.waresBatch = item.waresBatch.ToShortDateString();
                    supModel.waresquantity = item.waresquantity;
                    supModel.waresName = waresName;
                    supModel.waresSupportNum = item.waresSupportNum;
                    supModel.waresMinNum = item.waresMinNum;
                    listSup.Add(supModel);
                }
                return Json(new { total = count, rows = listSup });
            }

            if (name != "" && name != null && storeid !="" &&storeid!=null)
            {
                storeID = Convert.ToInt32(storeid);
                condition = x => x.storeid == storeID && x.validstatus == 1;
                var waresNameList = waresBll.Search(x => x.waresName.Contains(name)).Select(x => new { x.waresName, x.waresCode }).ToList();
                foreach (var item1 in waresNameList)
                {
                    condition = x => x.storeid == 10010 && x.validstatus == 1 && x.waresCode == item1.waresCode;
                    var list2 = bll.Search(condition).ToList().Select(x => new
                    {
                        x.waresID,
                        x.waresCode,
                        x.storeid,
                        x.waresBatch,
                        x.waresquantity,
                        x.waresSupportNum,
                        x.waresMinNum,
                        x.validstatus,
                    }).ToList();
                    foreach (var item2 in list2)
                    {
                        string storeName = storeBll.Search(x => x.storeid == item2.storeid).Select(x => new { x.storeName }).FirstOrDefault().storeName;
                        SupportModel supModel = new SupportModel();
                        supModel.waresID = item2.waresID;
                        supModel.waresCode = item2.waresCode;
                        supModel.storeName = storeName;
                        supModel.waresBatch = item2.waresBatch.ToShortDateString();
                        supModel.waresquantity = item2.waresquantity;
                        supModel.waresName = item1.waresName;
                        supModel.waresSupportNum = item2.waresSupportNum;
                        supModel.waresMinNum = item2.waresMinNum;
                        listSup.Add(supModel);
                    }

                }
                return Json(new { total = listSup.Count, rows = listSup });
            }

            if (name != "" && name != null&&string.IsNullOrEmpty(storeid))
            {
                var waresNameList = waresBll.Search(x => x.waresName.Contains(name)).Select(x => new { x.waresName, x.waresCode }).ToList();
                foreach (var item1 in waresNameList)
                {
                    condition = x => x.storeid == 10010 && x.validstatus == 1 && x.waresCode == item1.waresCode;
                    var list2 = bll.Search(condition).ToList().Select(x => new
                    {
                        x.waresID,
                        x.waresCode,
                        x.storeid,
                        x.waresBatch,
                        x.waresquantity,
                        x.waresSupportNum,
                        x.validstatus,
                        x.waresMinNum,
                    }).ToList();
                    foreach (var item2 in list2)
                    {
                        string storeName = storeBll.Search(x => x.storeid == item2.storeid).Select(x => new { x.storeName }).FirstOrDefault().storeName;
                        SupportModel supModel = new SupportModel();
                        supModel.waresID = item2.waresID;
                        supModel.waresCode = item2.waresCode;
                        supModel.storeName = storeName;
                        supModel.waresBatch = item2.waresBatch.ToShortDateString();
                        supModel.waresquantity = item2.waresquantity;
                        supModel.waresName = item1.waresName;
                        supModel.waresSupportNum = item2.waresSupportNum;
                        supModel.waresMinNum = item2.waresMinNum;
                        listSup.Add(supModel);
                    }

                }
                return Json(new { total = listSup.Count, rows = listSup });
            }
            //默认的情况下   (name != "" && name != null && storeid != 0 && storeid != null)
            else 
            {
                var list = bll.Search(condition, true, x => x.waresID, rows, page, out count).ToList().Select(x => new
                {
                    x.waresID,
                    x.waresCode,
                    x.storeid,
                    x.waresBatch,
                    x.waresquantity,
                    x.waresSupportNum,
                    x.waresMinNum,
                    x.validstatus,
                }).ToList();
                foreach (var item in list)
                {
                    waresName = waresBll.Search(x => x.waresCode == item.waresCode).Select(x => new { x.waresName }).FirstOrDefault().waresName;
                    string storeName = storeBll.Search(x => x.storeid == item.storeid).Select(x => new { x.storeName }).FirstOrDefault().storeName;
                    SupportModel supModel = new SupportModel();
                    supModel.waresID = item.waresID;
                    supModel.waresCode = item.waresCode;
                    supModel.storeName = storeName;
                    supModel.waresBatch = item.waresBatch.ToShortDateString();
                    supModel.waresquantity = item.waresquantity;
                    supModel.waresName = waresName;
                    supModel.waresSupportNum = item.waresSupportNum;
                    supModel.waresMinNum = item.waresMinNum;
                    listSup.Add(supModel);
                }
                return Json(new { total = count, rows = listSup });
            }

        }


        //获取选中的一条数据   
        public ActionResult GetOnlyOne(long id)
        {
            var list1 = new List<SupportModel>();
            // return Json(bll.Search(x => x.areaMaID == id).ToList().Select(x => new { x.areaMaID,x.userPhone, x.name, x.userName, x.userPass, x.areaCode, }).ToList()[0], JsonRequestBehavior.AllowGet);
            var list = bll.Search(x => x.waresID == id).ToList().Select(x => new { x.waresSupportNum,x.waresMinNum, x.waresID }).ToList();
            foreach (var item in list)
            {
                SupportModel model = new SupportModel();
                model.waresSupportNum = list.FirstOrDefault().waresSupportNum;
                model.waresID = list.FirstOrDefault().waresID;
                model.waresMinNum = list.FirstOrDefault().waresMinNum;
                list1.Add(model);
            }
            return Json(list1.ToList()[0], JsonRequestBehavior.AllowGet);
        }

        ////禁用用户  修改状态为
        public ActionResult ModifyState(waresdetails model)
        {
            string msg = "删除失败";
            string state = "false";
            if (bll.Delete(model) > 0)
            {
                msg = "删除成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }

        public ActionResult Modify2(waresdetails model)
        {
            string msg = "修改失败";
            string state = "false";

            var oneList = bll.Search(x => x.waresID == model.waresID).Select(x => new
            {
                x.waresID,
                x.waresCode,
                x.storeid,
                x.waresBatch,
                x.waresquantity,
                x.waresSupportNum,
                x.waresMinNum,
                x.validstatus,
            }).ToList();

            model.waresID = oneList.FirstOrDefault().waresID;
            model.waresCode = oneList.FirstOrDefault().waresCode;
            model.storeid = oneList.FirstOrDefault().storeid;
            model.waresBatch = oneList.FirstOrDefault().waresBatch;
            model.waresSupportNum = model.waresSupportNum;
            model.waresMinNum = model.waresMinNum;
            model.waresquantity = oneList.FirstOrDefault().waresquantity;
            model.validstatus = 1;

            if (model.waresSupportNum != 0)
            {
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                state = "false";
            }

            return Json(new { state = state, msg = msg });
        }

        public ActionResult GetShop()
        {
            return Json(storeBll.Search(x=>x.storeStatus==1).Select(x => new { shopID = x.storeid, shopName = x.storeName }).ToList(),JsonRequestBehavior.AllowGet);
        }
    }
}
